package me.ree.zkproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.ree.zkproduct.convert.PageConverter;
import me.ree.zkproduct.dto.PageQueryDTO;
import me.ree.zkproduct.entity.Product;
import me.ree.zkproduct.enums.DbStatusEnum;
import me.ree.zkproduct.mapper.ProductMapper;
import me.ree.zkproduct.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author ree
 * @since 2024-05-17
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;


    public Product getProductByCode(String productCode) {
        return productMapper.selectByProductCode(productCode);
    }

    public IPage<Product> getProductsPage(PageQueryDTO<Product> pageQueryDTO) {
        Page<Product> page = PageConverter.convertPageDto2MybatisPlusPage(pageQueryDTO);

        Product product = pageQueryDTO.getT();
        QueryWrapper<Product> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", DbStatusEnum.normal);
        if (product.getId() != null) {
            queryWrapper.eq("id", product.getId());
        }
        if (StringUtils.hasText(product.getProductName())) {
            queryWrapper.like("product_name", product.getProductName());
        }
        if (StringUtils.hasText(product.getProductCode())) {
            queryWrapper.eq("product_code", product.getProductCode());
        }
        if (product.getStock() != null) {
            queryWrapper.eq("stock", product.getStock());
        }
        if (product.getVersion() != null) {
            queryWrapper.eq("version", product.getVersion());
        }
        if (product.getGmtCreate() != null) {
            queryWrapper.eq("gmt_create", product.getGmtCreate());
        }
        if (product.getGmtModified() != null) {
            queryWrapper.eq("gmt_modified", product.getGmtModified());
        }
        if (pageQueryDTO.getGmtCreateStart() != null) {
            queryWrapper.ge("gmt_create", pageQueryDTO.getGmtCreateStart());
        }
        if (pageQueryDTO.getGmtCreateEnd() != null) {
            queryWrapper.le("gmt_create", pageQueryDTO.getGmtCreateEnd());
        }
        if (pageQueryDTO.getGmtModifiedStart() != null) {
            queryWrapper.ge("gmt_modified", pageQueryDTO.getGmtModifiedStart());
        }
        if (pageQueryDTO.getGmtModifiedEnd() != null) {
            queryWrapper.le("gmt_modified", pageQueryDTO.getGmtModifiedEnd());
        }

        queryWrapper.orderByDesc("gmt_create");
        // 多个字段排序
        // queryWrapper.orderByDesc("gmt_create").orderByAsc("product_code");

        return productMapper.selectPage(page, queryWrapper);
    }

    public int reduceStock(Product product) {
        return productMapper.reduceStock(product);
    }
}
